package top.wuyang.study.javawebjdbc.workexam;

import top.wuyang.study.javawebjdbc.dao.DBHelper;
import top.wuyang.study.javawebjdbc.worknews.TbNews;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class TbExamDAO {

    // 添加数据
    public static int insert(TbExam tbExam) throws Exception {
        Connection conn = DBHelper.getConnection();
        String sql = "INSERT INTO tb_exam_score (subject, student, score, level) VALUES (?, ?, ?, ?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, tbExam.getSubject());
        ps.setString(2, tbExam.getStudent());
        ps.setInt(3, tbExam.getScore());
        ps.setInt(4, tbExam.getLevel());
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }

    // 查询
    public static List<TbExam> query() throws Exception {
        List<TbExam> list = new ArrayList<>();
        Connection conn = DBHelper.getConnection();
        String sql = "select * from tb_exam_score";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            TbExam tbExam = new TbExam();
            tbExam.setId(rs.getInt("id"));
            tbExam.setSubject(rs.getString("subject"));
            tbExam.setStudent(rs.getString("student"));
            tbExam.setScore(rs.getInt("score"));
            tbExam.setLevel(rs.getInt("level"));
            list.add(tbExam);
        }
        conn.close();
        return list;
    }


    // 删除
    public static int delete(Integer id) throws Exception {
        Connection conn = DBHelper.getConnection();
        String sql = "delete from tb_exam_score where id = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,id);
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }


}
